﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using System.Data;

namespace DataAccess
{
    public partial class Comments
    {
        public int RowNumber { get; set; }
        public int ID { get; set; }
        public string UserName { get; set; }
        public int ProductID { get; set; }
        public string Comment { get; set; }
        public DateTime Date { get; set; }

        public static bool Add(Comments comment)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("Comment_Insert",
                    comment.UserName,comment.ProductID,comment.Comment,comment.Date);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }

        public static List<Comments> GetByProductID(int productID, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = 2;
                reader = DataProvider.Instance.ExecuteReader("Comment_ByProductID", productID, Convert.ToInt32(page), pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<Comments>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<Comments>();
            }
        }
    }
}
